package com.sparrow.source.common.dto.convert;

import com.sparrow.source.common.dto.FaceImage;
import com.sparrow.source.common.dto.LibMatInfo;
import com.sparrow.source.common.dto.UnvVerifyReport;
import com.sparrow.source.persistense.IasVerifyReport;
import com.sparrow.source.persistense.UnvDevice;
import com.sparrow.source.service.IUnvDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;

/**
 * @author bryceyung
 * @since 2024/11/25
 */
@Component
@Slf4j
public class UnvVerifyReportConvert {

    @Autowired
    private IUnvDeviceService deviceService;

    public IasVerifyReport convertIasVerifyReport(UnvVerifyReport unvVerifyReport, String respInfo) {
        IasVerifyReport verifyReport = new IasVerifyReport();
        UnvDevice device = deviceService.lambdaQuery().eq(UnvDevice::getSerialNumber, unvVerifyReport.getDeviceCode()).one();
        if (device == null) {
            log.error("[UnvVerifyReportConvert] Cannot find device info with the deviceCode:{}", unvVerifyReport.getDeviceCode());
            return null;
        }
        LibMatInfo libMatInfo = unvVerifyReport.getLibMatInfoList().get(0);
        verifyReport.setDeviceId(device.getId());
        verifyReport.setFaceId(libMatInfo.getMatchFaceId());
        verifyReport.setUid(libMatInfo.getMatchPersonId());
        verifyReport.setMatchRes(libMatInfo.getMatchStatus());
        verifyReport.setReportTime(new Timestamp(unvVerifyReport.getTimestamp() * 1000));
        verifyReport.setRespInfo(respInfo);
        verifyReport.setSeq(unvVerifyReport.getSeq());
        FaceImage faceImage = unvVerifyReport.getFaceInfoList().get(0).getFaceImage();
        if (faceImage == null) {
            return verifyReport;
        }
        verifyReport.setFaceData(faceImage.getData());
        return verifyReport;

    }

}
